<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>库存调拨单管理</title>
		<link rel="stylesheet" href="/static/layui/css/layui.css" />
<link rel="stylesheet" href="/static/ui/css/ui.css" />
	</head>
	<body class="pear-container">
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<div class="layui-input-inline" style="width: 100px;">
							<input type="text" name="name" placeholder="商品名称" class="layui-input">
						</div>
						<div class="layui-input-inline" style="width: 100px;">
							<input type="text" name="model" placeholder="型号" class="layui-input">
						</div>
						<div class="layui-input-inline" style="width: 100px;">
							<input type="number" name="trans_no" placeholder="调拨单号" class="layui-input">
						</div>
						<div class="layui-input-inline" style="width: 100px;">
							<select name="status">
								<option value="">选择状态</option>
								<option value="1">待审核</option>
								<option value="2">待接收</option>
								<option value="3">已完结</option>
							</select>
						</div>
						<div class="layui-input-inline" style="width: 100px;">
							<select name="cate_id" lay-search id="cate-area">
								<option value="">选择分类</option>
							</select>
						</div>
						<div class="layui-input-inline" style="width: 100px;">
							<select name="from_area_id" lay-search id="from-area">
								<option value="">发起仓库</option>
							</select>
						</div>
						<div class="layui-input-inline" style="width: 100px;">
							<select name="to_area_id" lay-search id="to-area">
								<option value="">接收仓库</option>
							</select>
						</div>
						<div class="layui-input-inline">
							<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit
								lay-filter="stock_transfer-query">
								<i class="layui-icon layui-icon-search"></i>
								查询
							</button>
							<button type="reset" class="pear-btn pear-btn-md" lay-filter="stock_transfer-reset"
								lay-submit>
								<i class="layui-icon layui-icon-refresh"></i>
								重置
							</button>
						</div>
					</div>
				</form>
			</div>
		</div>
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="stock_transfer-table" lay-filter="stock_transfer-table"></table>
			</div>
		</div>

		<script type="text/html" id="stock_transfer-toolbar">
			<button class="layui-btn layui-btn-primary layui-border-red layui-btn-sm" lay-event="start">
				<i class="layui-icon layui-icon-export"></i>
				发起调拨单
			</button>
			<button class="layui-btn layui-btn-primary  layui-border-blue layui-btn-sm" lay-event="check">
				<i class="layui-icon layui-icon-time"></i>
				审核调拨单
			</button>
			<button class="layui-btn layui-btn-primary layui-border-green layui-btn-sm" lay-event="in">
				<i class="layui-icon layui-icon-add-1"></i>
				接收调拨单
			</button>
		</script>

		<script type="text/html" id="stock_transfer-bar">
		{{# if(d.status == 1){ }}
			<button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="remove">
				<i class="layui-icon layui-icon-delete"></i>
			</button>
			{{# } }}
		</script>
		<script type="text/html" id="stock_transfer-status">
		{{# if(d.status == 1){ }}
			<button class="layui-btn layui-btn-primary layui-btn-xs">待审核</button>
			{{# }else if(d.status == 2) { }}
				<button class="layui-btn layui-btn-danger layui-btn-xs">待接收</button>
				{{# }else if(d.status == 3) { }}
					<button class="layui-btn layui-btn-sm">已完结</button>
					{{# } }}
		</script>
		<script src="../config.js"></script>
		<script src="/static/layui/layui.js"></script>
		<script src="/static/ui/ui.js"></script>
		<script>
			layui.use(['table', 'form', 'jquery', 'common'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let common = layui.common;

				let MODULE_PATH = "./";
				let cats = [],
					area = [];

				let cols = [
					[{
							title: 'ID',
							field: 'id',
							align: 'center',
							minWidth : '60',
						},
						{
							title: '商品名称',
							field: 'goods_name',
							align: 'center',
							minWidth : '120',
						},

						{
							title: '数量',
							field: 'trans_num',
							align: 'center',
							minWidth : '80'
						},
						{
							title: '型号',
							field: 'model',
							align: 'center',
							minWidth : '80',
						},
						{
							title: '分类',
							field: 'cname',
							align: 'center',
							minWidth : '120',
						},
						{
							title: '状态',
							field: 'status',
							align: 'center',
							minWidth : '100',
							templet: '#stock_transfer-status'
						},
						{
							title: '发起库',
							field: 'from_areaname',
							align: 'center',
							minWidth : '80',
						},
						{
							title: '接收库',
							field: 'to_areaname',
							align: 'center',
							minWidth : '80',
						},

						{
							title: '添加时间',
							field: 'add_time',
							align: 'center',
							minWidth : '120',
							//templet: '#stock_transfer-time'
						},

						{
							title: '操作',
							toolbar: '#stock_transfer-bar',
							align: 'center',
							width: 80
						},
						{
							title: '调拨单号',
							field: 'trans_no',
							align: 'center',
							minWidth: 150,
							fixed: 'right'
						},
					]
				]

				table.render({
					elem: '#stock_transfer-table',
					url: apiUrl + 'stock_transfer/list',
					headers: getHeader(),
					parseData: function(res) {
						//console.log(res)
						cats = res.data.cates;
						area = res.data.area;
						return {
							"code": res.code, //解析接口状态
							"msg": res.message, //解析提示文本
							"count": res.data.count, //解析数据长度
							"data": res.data.list //解析数据列表
						};
					},
					page: true,
					cols: cols,
					skin: 'line',

					toolbar: '#stock_transfer-toolbar',
					defaultToolbar: [{
						title: '刷新',
						layEvent: 'refresh',
						icon: 'layui-icon-refresh',
					}, 'filter', 'print', 'exports']
				});
				setTimeout(() => {
					let cateHtml = `<option value="">选择分类</option>`,
						areaHtml1 = `<option value="">发起仓库</option>`,
						areaHtml2 = `<option value="">接收仓库</option>`;
					cats.forEach(d => {
						cateHtml += `<option value='${d.id}'>${d.name}</option>`
					})
					area.forEach(el => {
						areaHtml1 += `<option value='${el.id}'>${el.name}</option>`
						areaHtml2 += `<option value='${el.id}'>${el.name}</option>`
					})
					$("#cate-area").html(cateHtml)
					$("#from-area").html(areaHtml1)
					$("#to-area").html(areaHtml2)
					form.render("select");
				}, 1000)
				table.on('tool(stock_transfer-table)', function(obj) {
					if (obj.event === 'remove') {
						window.remove(obj);
					} else if (obj.event === 'edit') {
						window.edit(obj);
					}
				});

				table.on('toolbar(stock_transfer-table)', function(obj) {
					if (obj.event === 'start') {
						window.start();
					} else if (obj.event === 'check') {
						window.check();
					} else if (obj.event === 'in') {
						window.in();
					}
				});
				// 监听搜索操作

				form.on('submit(stock_transfer-query)', function(data) {
					table.reload('stock_transfer-table', {
						where: {
							param: $('form').serialize()
						},
						page: {
							curr: 1
						}
					})
					return false;
				});
				form.on('submit(stock_transfer-reset)', function(data) {
					table.reload('stock_transfer-table', {
						where: {
							param: ''
						},
						page: {
							curr: 1
						}
					});
					return false;
				})

				window.start = function() {
					layer.open({
						type: 2,
						title: '发起调拨单',
						shade: 0.1,
						offset: 'rt',
						area: ['98%', '100%'],
						anim: 1,
						maxmin: true,
						content: MODULE_PATH + 'start.html'
					});
				}

				// window.edit = function(obj) {
				// 	layer.open({
				// 		type: 2,
				// 		title: '修改',
				// 		shade: 0.1,
				// 		offset: 'rt',
				// 		area: ['80%', '100%'],
				// 		anim: 1,
				// 		maxmin: true,
				// 		content: MODULE_PATH + 'edit.html?id=' + obj.data.id
				// 	});
				// }
				window.remove = function(obj) {
					layer.confirm('确定要删除', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						_post(layui, 'stock_transfer/del', {
							id: obj.data.id
						}, res => {
							layer.msg('删除成功', {
								icon: 1,
								time: 1000
							}, () => {
								obj.del();
							});
						}, err => {
							//layer.msg('删除失败',{icon:2,time:1000});
						})
					});
				}
				window.check = () => {
					layer.prompt({
						formType: 2,
						value: '',
						title: '审核-输入调拨单号',
						area: ['150px', '22px'] //自定义文本域宽高
					}, function(value, index, elem) {

						if (value < 1 || isNaN(value)) {
							layer.msg('请输入正确的调拨单号');
							return false;
						}
						let postData = {
							trans_no: value
						}
						//console.log(postData)
						_post(layui, 'stock_transfer/searchTransNo', postData, res => {
							//layer.msg('')
							//fromTable.reload()
							//layer.close(index);
							//console.log(res)
							if (res.status != 1) {
								layer.msg('调拨单已审核')
								return false;
							}

							layer.close(index);
							layer.open({
								type: 2,
								title: '审核调拨单',
								shade: 0.1,
								offset: 'rt',
								area: ['80%', '100%'],
								anim: 1,
								maxmin: true,
								content: MODULE_PATH + 'check.html?trans_no=' + value
							});

						})

					});
				}
				
				window.in = () => {
					layer.prompt({
						formType: 2,
						value: '',
						title: '接收-输入调拨单号',
						area: ['150px', '22px'] //自定义文本域宽高
					}, function(value, index, elem) {
				
						if (value < 1 || isNaN(value)) {
							layer.msg('请输入正确的调拨数');
							return false;
						}
						let postData = {
							trans_no: value
						}
						//console.log(postData)
						_post(layui, 'stock_transfer/searchTransNo', postData, res => {
							//layer.msg('')
							//fromTable.reload()
							//layer.close(index);
							//console.log(res)
							if (res.status != 2) {
								layer.msg('调拨单状态错误')
								return false;
							}
				
							layer.close(index);
							layer.open({
								type: 2,
								title: '接收调拨单',
								shade: 0.1,
								offset: 'rt',
								area: ['80%', '100%'],
								anim: 1,
								maxmin: true,
								content: MODULE_PATH + 'in.html?trans_no=' + value + '&to_area_id=' + res.to_area_id
							});
				
						})
				
					});
				}
				
				window.refresh = function(param) {
					table.reload('stock_transfer-table');
				}
			})
		</script>
	</body>
</html>
